package com.rlyy.basic.trace;

import com.rlyy.common.param.LinkTrace;
import org.springframework.core.NamedThreadLocal;
import org.springframework.lang.Nullable;

/**
 * @author yuanqinglong
 * @since 2021/10/13 11:28
 */
public class TraceContextHolder {


    private static final ThreadLocal<LinkTrace> TRACE_ATTRIBUTES = new NamedThreadLocal("Request Trace attributes");

    protected static void resetRequestAttributes() {
        TRACE_ATTRIBUTES.remove();
    }

    public static LinkTrace getResetRequestAttributes() {
        LinkTrace trace = TRACE_ATTRIBUTES.get();
        TRACE_ATTRIBUTES.remove();
        return trace;
    }


    protected static void setTraceAttributes(@Nullable LinkTrace linkTrace) {
        if (linkTrace == null) {
            resetRequestAttributes();
        } else {
            TRACE_ATTRIBUTES.set(linkTrace);
        }
    }

    public static LinkTrace getLinkTraceAttributes() {
        return TRACE_ATTRIBUTES.get();
    }

}
